<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      
      <el-form-item label="出发地" prop="chufa">
        <el-input
          v-model="queryParams.chufa"
          placeholder="请输入出发地"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="目的地" prop="mudidi">
        <el-input
          v-model="queryParams.mudidi"
          placeholder="请输入目的地"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ziliao:chanpin:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ziliao:chanpin:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ziliao:chanpin:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ziliao:chanpin:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="chanpinList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="所属旅行社" align="center" prop="lid" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="金额" align="center" prop="jine" />
      <el-table-column label="出发地" align="center" prop="chufa" />
      <el-table-column label="目的地" align="center" prop="mudidi" />
      <el-table-column label="最大人数" align="center" prop="maxpeople" />
      <el-table-column width="200px" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.tuijian==0"
            icon="el-icon-edit"
            @click="editTuijian(scope.row)"
            v-hasPermi="['ziliao:yanxuetuijian:add']"
          >添加到推荐</el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.tuijian>0"
            icon="el-icon-edit"
            @click="delTuijian(scope.row)"
            v-hasPermi="['ziliao:yanxuetuijian:add']"
          >移除推荐</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ziliao:chanpin:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ziliao:chanpin:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改研学产品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="金额" prop="jine">
          <el-input v-model="form.jine" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="标签" prop="tags">
          <el-input v-model="form.tags" placeholder="请输入标签" />
        </el-form-item>
        <el-form-item label="出发地" prop="chufa">
          <el-input v-model="form.chufa" placeholder="请输入出发地" />
        </el-form-item>
        <el-form-item label="目的地" prop="mudidi">
          <el-input v-model="form.mudidi" placeholder="请输入目的地" />
        </el-form-item>
        <el-form-item label="最大人数" prop="maxpeople">
          <el-input v-model="form.maxpeople" placeholder="请输入最大人数" />
        </el-form-item>
        <el-form-item label="轮播图" prop="pics">
          <image-upload :limit="9" v-model="form.pics"/>
        </el-form-item>
        <el-form-item label="概述">
          <editor v-model="form.gaishu" :min-height="192"/>
        </el-form-item>
        <el-form-item label="费用">
          <editor v-model="form.feiyong" :min-height="192"/>
        </el-form-item>
        <el-form-item label="详情">
          <editor v-model="form.xiangqing" :min-height="192"/>
        </el-form-item>
        <el-form-item label="须知">
          <editor v-model="form.xuzhi" :min-height="192"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改研学推荐对话框 -->
    <el-dialog title="添加到推荐" :visible.sync="tjopen" width="1000px" :close-on-click-modal="false" append-to-body>
      <el-form ref="tuijian" :model="tuijian" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="tuijian.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="推荐图" prop="pic">
          <image-upload :limit="1" v-model="tuijian.pic"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitTuijian">确 定</el-button>
        <el-button @click="cancelTuijian">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listChanpin, getChanpin, delChanpin, addChanpin, updateChanpin } from "@/api/ziliao/chanpin";
import { listYanxuetuijian, getYanxuetuijian, delYanxuetuijian, addYanxuetuijian, updateYanxuetuijian } from "@/api/ziliao/yanxuetuijian";
export default {
  name: "Chanpin",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 研学产品表格数据
      chanpinList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      tjopen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        lid: null,
        uid: null,
        title: null,
        jine: null,
        tags: null,
        chufa: null,
        mudidi: null,
        maxpeople: null,
        pics: null,
        gaishu: null,
        feiyong: null,
        xiangqing: null,
        xuzhi: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      tuijian:{
        title:'',
        type:'cp',
        pic:''
      },
    };
  },
  created() {
    const id = this.$route.params && this.$route.params.id;
    if (id) {
      this.queryParams.lid = id
      this.loading = true;
      listChanpin(this.queryParams).then(response => {
          this.chanpinList = response.rows;
          this.total = response.total;
          this.loading = false;
      })
    }
  },
  methods: {
    editTuijian(row){
      this.tjopen = true;
      this.tuijian = {
        title:'',
        type:'cp',
        pic:'',
        bid:row.id,
        shi:row.chufa
      };
    },
    delTuijian(row){
      const id = row.tuijian
      this.$modal.confirm('是否确认移除推荐？').then(function() {
        return delYanxuetuijian(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("移除成功");
      }).catch(() => {});
    },
    cancelTuijian(){
      this.tjopen = false;
      this.tuijian = {
        title:'',
        type:'lxs',
        pic:''
      };
    },
    submitTuijian(){
      this.$refs["tuijian"].validate(valid => {
        if (valid) {
          console.log(this.tuijian)
          addYanxuetuijian(this.tuijian).then(response => {
              this.$modal.msgSuccess("推荐成功");
              this.tjopen = false;
              this.getList();
          });
        }
      });
    },
    /** 查询研学产品列表 */
    getList() {
      this.loading = true;
      listChanpin(this.queryParams).then(response => {
        this.chanpinList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        lid: null,
        uid: null,
        title: null,
        jine: null,
        tags: null,
        chufa: null,
        mudidi: null,
        maxpeople: null,
        pics: null,
        gaishu: null,
        feiyong: null,
        xiangqing: null,
        xuzhi: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加研学产品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getChanpin(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改研学产品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.lid = this.queryParams.lid
          if (this.form.id != null) {
            updateChanpin(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addChanpin(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除研学产品编号为"' + ids + '"的数据项？').then(function() {
        return delChanpin(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ziliao/chanpin/export', {
        ...this.queryParams
      }, `chanpin_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
